package com.weme.common.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.ImageView
import com.weme.common.R
import com.weme.common.databinding.CustomTopNavigationBinding
import com.weme.common.util.UtilsApp
import com.weme.common.util.setOnFastClickListener
import com.weme.common.util.visible
import kotlin.properties.Delegates

interface CustomTopNavigationBarListener {
    fun onMoreButtonClicked()
}

class CustomTopNavigationBar @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, defStyle: Int = 0) : FrameLayout(context, attributeSet, defStyle) {
    private val viewBinding = CustomTopNavigationBinding.inflate(LayoutInflater.from(context), this, true)

    init {
        viewBinding.ibBack.setOnFastClickListener {
            UtilsApp.back()
        }
        val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CustomTopNavigationBar, defStyle, 0)
        val title = typedArray.getString(R.styleable.CustomTopNavigationBar_title)
        viewBinding.tvTitle.text = title
        val moreResource = typedArray.getResourceId(R.styleable.CustomTopNavigationBar_moreResource, 0)
        moreResource.takeIf { it != 0 }?.let {
            viewBinding.ibMore.setImageResource(it)
            viewBinding.ibMore.visible()
        }

        typedArray.recycle()
    }

    var listener: CustomTopNavigationBarListener? by Delegates.observable(null) { _, _, newValue ->
        viewBinding.ibMore.setOnFastClickListener {
            newValue?.onMoreButtonClicked()
        }

    }
    fun setTitle(title:String){
        viewBinding.tvTitle.text = title
    }
    fun getMore(): ImageView {
        return viewBinding.ibMore
    }

}